set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open the Trace file
set file1 [open trace.tr w]
$ns trace-all $file1

#Open the NAM trace file
set file2 [open trace.nam w]
$ns namtrace-all $file2

#Define a 'finish' procedure
proc finish {} {
        global ns file1 file2
        $ns flush-trace
        close $file1
        close $file2
        exec nam trace.nam &
        exit 0
}

#Next line should be commented out to have the static routing
$ns rtproto DV

#Creat network nodes
#n0 represents the Internet
set n0 [$ns node]
$n0 shape "hexagon"
#n1, n2 and n3 represent boundary router
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#n4 represents internal switch
set n4 [$ns node]
#n5 represent DMZ switch
set n5 [$ns node]
#n6, n7, n8 and n9 represent internal switch
set n6 [$ns node]
set n7 [$ns node]
set n8 [$ns node]
set n9 [$ns node]
#n10 represents cable modem
set n10 [$ns node]
#n11, n12, n13 and n14 represent servers
set n11 [$ns node]
$n11 shape "box"
set n12 [$ns node]
$n12 shape "box"
set n13 [$ns node]
$n13 shape "box"
set n14 [$ns node]
$n14 shape "box"
#n15 and n16 represent finance users
set n15 [$ns node]
$n15 shape "box"
set n16 [$ns node]
$n16 shape "box"
#n17 and n18 represent developers
set n17 [$ns node]
$n17 shape "box"
set n18 [$ns node]
$n18 shape "box"
#n19 and n20 represent administrators
set n19 [$ns node]
$n19 shape "box"
set n20 [$ns node]
$n20 shape "box"
#n21, n22 and n23 represent regular inside users
set n21 [$ns node]
$n21 shape "box"
set n22 [$ns node]
$n22 shape "box"
set n23 [$ns node]
$n23 shape "box"
#n24 represents a partner user
set n24 [$ns node]
$n24 shape "box"
#n25 represents a sales office user
set n25 [$ns node]
$n25 shape "box"

#Create duplex links between the nodes
$ns duplex-link $n0 $n1 44.74Mb 10ms DropTail
$ns duplex-link $n0 $n2 44.74Mb 10ms DropTail
$ns duplex-link $n0 $n3 1.544Mb 10ms DropTail
$ns duplex-link $n0 $n10    2Mb 10ms DropTail

$ns duplex-link $n1 $n4 100Mb 10ms DropTail
$ns duplex-link $n1 $n5 100Mb 10ms DropTail
$ns duplex-link $n2 $n4 100Mb 10ms DropTail
$ns duplex-link $n2 $n5 100Mb 10ms DropTail

$ns duplex-link $n4 $n6 100Mb 10ms DropTail
$ns duplex-link $n4 $n7 100Mb 10ms DropTail
$ns duplex-link $n4 $n8 100Mb 10ms DropTail
$ns duplex-link $n4 $n9 100Mb 10ms DropTail

$ns duplex-link $n5 $n11 100Mb 10ms DropTail
$ns duplex-link $n5 $n12 100Mb 10ms DropTail
$ns duplex-link $n5 $n13 100Mb 10ms DropTail
$ns duplex-link $n5 $n14 100Mb 10ms DropTail

$ns duplex-link $n6 $n15 100Mb 10ms DropTail
$ns duplex-link $n6 $n16 100Mb 10ms DropTail

$ns duplex-link $n8 $n17 100Mb 10ms DropTail
$ns duplex-link $n8 $n18 100Mb 10ms DropTail

$ns duplex-link $n9 $n19 100Mb 10ms DropTail
$ns duplex-link $n9 $n20 100Mb 10ms DropTail

$ns duplex-link $n7 $n21 100Mb 10ms DropTail
$ns duplex-link $n7 $n22 100Mb 10ms DropTail
$ns duplex-link $n7 $n23 100Mb 10ms DropTail

$ns duplex-link $n3 $n24 100Mb 10ms DropTail

$ns duplex-link $n10 $n25 100Mb 10ms DropTail

$ns cost $n0 $n1 3
$ns cost $n0 $n2 2

#Give node position (for NAM)
#$ns duplex-link-op $n0 $n1 orient right
#$ns duplex-link-op $n0 $n2 orient right-up
#$ns duplex-link-op $n1 $n3 orient right


#Setup TCP connections for normal traffic
set src1 [new Agent/TCP]
set src2 [new Agent/TCP]
set src3 [new Agent/TCP]
set src4 [new Agent/TCP]
set src5 [new Agent/TCP]
set src6 [new Agent/TCP]

set sink1 [new Agent/TCPSink]
set sink2 [new Agent/TCPSink]
set sink3 [new Agent/TCPSink]
set sink4 [new Agent/TCPSink]
#set sink5 [new Agent/TCPSink]
#set sink6 [new Agent/TCPSink]

$n24 attach $src1 7771
$n25 attach $src2 7772
$n19 attach $src3 7773
$n17 attach $src4 7774
$n22 attach $src5 7775
$n16 attach $src6 7776

$n14 attach $sink1 80
$n13 attach $sink2 3306
$n11 attach $sink3 143
$n12 attach $sink4 53
#$n13 attach $sink5 3306
#$n11 attach $sink6 143

$sink1 listen;
$sink2 listen;
$sink3 listen;
$sink4 listen;
#$sink5 listen;
#$sink6 listen;

#$src1 set window_ 100
#$src1 set fid_ 1

$ns connect $src1 $sink1
$ns connect $src2 $sink2
$ns connect $src3 $sink3
$ns connect $src4 $sink4
$ns connect $src5 $sink2
$ns connect $src6 $sink3

# set network layer (IP) flow ID, to give them blue color
$src1 set fid_ 1
$src2 set fid_ 1
$src3 set fid_ 1
$src4 set fid_ 1
$src5 set fid_ 1
$src6 set fid_ 1

#Setup TCP connections for attacks
set attack1 [new Agent/TCP]
set attack2 [new Agent/TCP]
set attack3 [new Agent/TCP]

set victim1 [new Agent/TCPSink]
#set victim2 [new Agent/TCPSink]
#set victim3 [new Agent/TCPSink]

$n21 attach $attack1 911
$n22 attach $attack2 911
$n18 attach $attack3 911

$n22 attach $victim1 7777
#$n13 attach $victim2 3306
#$n11 attach $victim3 143

$victim1 listen;
#$victim2 listen;
#$victim3 listen;

$ns connect $attack1 $victim1
$ns connect $attack2 $sink2
$ns connect $attack3 $sink3

# set network layer (IP) flow ID, to give them red color
$attack1 set fid_ 2
$attack2 set fid_ 2
$attack3 set fid_ 2

#Setup FTP applications to simulate traffic over TCP connections
set ftp1 [new Application/FTP]
set ftp2 [new Application/FTP]
set ftp3 [new Application/FTP]
set ftp4 [new Application/FTP]
set ftp5 [new Application/FTP]
set ftp6 [new Application/FTP]

set ftp_att1 [new Application/FTP]
set ftp_att2 [new Application/FTP]
set ftp_att3 [new Application/FTP]

$ftp1 attach-agent $src1
$ftp2 attach-agent $src2
$ftp3 attach-agent $src3
$ftp4 attach-agent $src4
$ftp5 attach-agent $src5
$ftp6 attach-agent $src6

$ftp_att1 attach-agent $attack1
$ftp_att2 attach-agent $attack2
$ftp_att3 attach-agent $attack3


proc traffic {app start_time stop_time} {
    global ns
    $ns at $start_time "$app start"
    $ns at $stop_time "$app stop"
}


#Start simulation
traffic $ftp1 0.1 5.0
traffic $ftp2 0.1 5.0
traffic $ftp3 0.1 5.0
traffic $ftp4 0.1 5.0
traffic $ftp5 0.1 5.0
traffic $ftp6 0.1 5.0

traffic $ftp_att1 1.0 1.5
traffic $ftp_att2 2.0 4.5
traffic $ftp_att3 1.0 4.0

$ns at 6.0 "finish"

$ns run

